﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Configuration.Settings;
using ASPNETVN.PORTAL.Components.UI.DataTypes;
using ASPNETVN.PORTAL.Components.Helpers;
using System.Collections;
using ASPNETVN.PORTAL.Components.Share;
using ASPNETVN.PORTAL.Modules.INTRODUCTION.Controls;

namespace ASPNETVN.PORTAL.Modules.INTRODUCTION.MENU
{
    public partial class Main : PortalModuleControl
    {
        public Main()
        {
            ModuleSettingItem itemSetting = new ModuleSettingItem(new StringDataType());
            itemSetting.Value = Guid.Empty.ToString();
            itemSetting.Name = "ModuleID của Module INTRODUCTION";
            itemSetting.Description = "ModuleID của Module INTRODUCTION";
            itemSetting.SortOrder = 1;
            this.BaseSettings.Add("INTRODUCTION.ModuleID", itemSetting);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Hashtable settings = this.Settings;
            Guid moduleID = new Guid(settings["INTRODUCTION.ModuleID"].ToString());
            this.ModuleTitle.AddUrl = HttpUrlBuilder.BuildShareUrl(moduleID, 0, "CategoryManager");

            this.BuildCategory();
        }

        private void BuildCategory()
        {
            Hashtable settings = this.Settings;
            Guid moduleID = new Guid(settings["INTRODUCTION.ModuleID"].ToString());
            if (moduleID == Guid.Empty)
            {
                ContentPane.Controls.Add(new LiteralControl("Chưa cấu hình INTRODUCTION.ModuleID"));
                return;
            }

            // Left Pane.
            LiteralControl literal = new LiteralControl("");
            literal.Text += "<div class='MenuLink'>";
            
            IList<Category> allCategories = Category.SelectCollectionAll(moduleID);
            IList<Category> topCategories = allCategories.Where(x => x.ParentID == 0).OrderBy(x => x.SortOrder).ToList();
            string url = string.Empty;
            foreach (Category topCategory in topCategories)
            {
                url = HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.Item, "categoryid={0}", topCategory.ID);
                url = url.Substring(2);
                literal.Text += string.Format("<div class='MenuLinkGroup'><a href='{0}'>{1}</a></div>", url, topCategory.Title);
                Category category = topCategory;
                IList<Category> childCategories = allCategories.Where(x => x.ParentID == category.ID).OrderBy(x => x.SortOrder).ToList();

                foreach (Category childCategory in childCategories)
                {
                    url = HttpUrlBuilder.BuildModulePageUrl(moduleID, 0, ModulePages.Item, "categoryid={0}", childCategory.ID);
                    url = url.Substring(2);

                    literal.Text += string.Format("<a class='MenuLinkItem' href='{0}'>» {1}</a>", url, childCategory.Title);
                }
            }
            literal.Text += "</div>";
            ContentPane.Controls.Add(literal);
        }
    }
}